package com.lottery.shadowbd.ui.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.widget.LoadingDialog;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * Created by Administrator on 2018/3/5 0005.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected Context mContext;

    //进度加载框
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setTranslucentStatus();

        initView(savedInstanceState);
        initData();
        initListener();
    }

    /**
     * 抽象方法子类必须实现
     */
    protected abstract void initView(Bundle savedInstanceState);

    protected abstract void initData();

    protected abstract void initListener();

    public abstract void widgetClick(View v);

    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (view != null && isShouldHideKeyBoard(view, ev)) {
                hideKeyboard(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void setTranslucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.colorMain);
        }
    }

    //隐藏软键盘
    protected void hideKeyboard(IBinder windowToken) {
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        if (im.isActive() && windowToken != null) {
            im.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    //判断是否需要隐藏软键盘
    private boolean isShouldHideKeyBoard(View view, MotionEvent ev) {
        if (view != null && (view instanceof EditText)) {
            int[] l = {0, 0};
            view.getLocationInWindow(l);
            int left = l[0];
            int top = l[1];
            int bottom = top + view.getHeight();
            int right = left + view.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom) {
                //点击EditText事件，忽略
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    //显示加载进度
    protected void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(BaseActivity.this)
                    .builder()
                    .setCanceledOnTouchOutside(false)
                    .setCanceled(false);
        } else if(!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    protected void dismissDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        mContext = null;
        super.onDestroy();
    }
}
